package com.gateway.server.listener;


import com.gateway.common.dto.sync.RouteSyncData;
import com.gateway.common.enums.DataEventTypeEnum;
import com.gateway.repository.bean.GatewayRouteDefinition;

/**
 * @program: ahcloud-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2023/2/6 15:53
 **/
public abstract class AbstractListDataChangedListener implements DataChangedListener {

    private final ChangeData changeData;

    protected AbstractListDataChangedListener(ChangeData changeData) {
        this.changeData = changeData;
    }

    @Override
    public void onRouteChanged(GatewayRouteDefinition routeDefinition, DataEventTypeEnum eventType) {
        RouteSyncData routeSyncData = new RouteSyncData(routeDefinition.getRouteId(), eventType, routeDefinition.getVersion(), routeDefinition.getEnv());
        publishConfig(changeData.getRouteDataId(), routeSyncData);
    }

    /**
     * 发布配置
     * @param dataId
     * @param data
     */
    protected abstract void publishConfig(String dataId, Object data);


    public static class ChangeData {

        /**
         * route definition data id
         */
        private final String routeDataId;

        public ChangeData(String routeDataId) {
            this.routeDataId = routeDataId;
        }

        public String getRouteDataId() {
            return routeDataId;
        }
    }
}
